 <?php
 class db_Mysql
 {
  
  var $usepconnect;
  var $dbSever;
  var $dbDatabase; 
  var $dbbase;
  var $dbUser;
  var $dbPwd;
  var $dbLink;
  var $result;// 执行query命令的指针
  var $num_rows;// 返回的条目数
  var $insert_id;// 传回最后一次使用 INSERT 指令的 ID
  var $affected_rows;// 传回query命令所影响的列数目
                  // INSERT、UPDATE 或 DELETE 所影响的列 (row) 数目。
                  // delete 如果不带where，那么则返回0
  function dbconnect()
  {
   global $usepconnect;
   if($usepconnect==1)
    $this->dbLink=@mysql_pconnect($this->dbServer,$this->dbUser,$this->dbPwd);//long connect
   else
    $this->dbLink=@mysql_connect($this->dbServer,$this->dbUser,$this->dbPwd);//short connect
   //$this->dbhalt($this->dbbase);
   if(!$this->dbLink) $this->dbhalt("exsiting error when connecting!");
   if($this->dbbase=="") $this->dbbase=$this->dbDatabase;
   if(!@mysql_select_db($this->dbbase,$this->dbLink))
    $this->dbhalt("can't use this database,please check database!");
   mysql_query("SET NAMES utf8");
  } // end func() connect database
  
  //change database
  function dbchange_db($dbbase){
   $this->dbconnect($dbbase);
  
  }
  
  //*执行SQL语句
  function execute($sql){
   //if($dbbase!="") $this->dbchange_db($dbbase);
   $this->result=mysql_query($sql);
  // if(!$this->result) $this->dbhalt("wrong sql sentence!".$sql);
   return $this->result;
  }


function execute_first($sql,$dbbase){
   $result=execute($sql,$dbbase);
   $returnarray=mysql_fetch_array($result);
   $this->num_rows=mysql_num_rows($result);
   $this->free_result($result);
   return $returnarray;
  }

function fetch_array($result)
	{
		//$result = $this->result;
		return mysql_fetch_array($result);
	}

function fetch_row($result)
	{
		return mysql_fetch_row($result);
	}
//*直接返回记录集行数
function get_rows($sql)
	{
		return mysql_num_rows(mysql_query($sql));
	}

  //*返回行数************************
function num_rows($result)
	{
		//$result = $this->result;
		return mysql_num_rows($result);
	}

function data_seek($result,$rowNumber){
	//$result = $this->result;
	return mysql_data_seek($result,$rowNumber);
	}
	
///////////
  function dbhalt($errmsg){
   $msg="database is wrong!";
   $msg=$errmsg;
   echo"$msg";
   die();
  }

 function delete($sql){
   $result=$this->execute($sql,$dbbase);
   $this->affected_rows=mysql_affected_rows($this->dbLink);
   $this->free_result($result);
   return $this->affected_rows;
 }
  
function insert($sql){
$result=$this->execute($sql,$dbbase);
$this->insert_id=mysql_insert_id($this->dbLink);
$this->free_result($result);
 return $this->insert_id;
}
  
  function update($sql){
   $result=$this->execute($sql,$dbbase);
   $this->affected_rows=mysql_affected_rows($this->dbLink);
   $this->free_result($result);
    return $this->affected_rows;
  }

  function count_records($table,$index="id",$where="",$dbbase){
   if($dbbase!="")$this->dbchangedb($dbbase);
   $result=@mysql_query("select count(".$index.") as 'num' form".$table."where".$where,$this->dbLink);
   if(!$result) $this->dbhalt("wrong sql sentence".$sql);
    $num=@mysql_result($result,0,"num");
   return $num;
  }
  
  function get_num($result){
   $num=@mysql_numrows($result);
   return $num;
  }
 
  function free_result($result){
   @mysql_free_result($result);
  }

function dbclose(){
   mysql_close($this->dbLink);
  }
 }

// end class
?>

